import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OutsourcingWorkFlowServiceProxy, WorkflowServiceProxy, OutsouringServiceProxy, UserServiceProxy, OutsourcingViewDto,ProcessingOrderServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { FlowworkauditDetailModelComponent } from '../flowworkauditDetailModel/flowworkauditDetailModel.component';
import { ProcessingOrderListComponent } from '../ProcessingOrderList/ProcessingOrderList.component';
import { OutsourcingApplyChangeListComponent } from '../OutsourcingApplyChangeList/OutsourcingApplyChangeList.component';
import { ResourceLoader } from '@angular/compiler';



@Component({
  selector: 'app-taskoutsouring',
  templateUrl: './taskoutsouring.component.html',
  styleUrls: ['./taskoutsouring.component.css']
})
export class TaskoutsouringComponent extends AppComponentBase {
  @ViewChild('flowworkauditdetailmodel') flowworkauditdetailmodel: FlowworkauditDetailModelComponent;
  @ViewChild('processingOrderList') processingOrderList: ProcessingOrderListComponent;
  @ViewChild('OutsourcingApplyChangeList') OutsourcingApplyChangeList: OutsourcingApplyChangeListComponent;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  filterText = '';
  advancedFiltersAreShown = false;

  outsourcings: OutsourcingViewDto[];
  constructor(injector: Injector,
    private _outsourcingWorkFlowServiceProxy: OutsourcingWorkFlowServiceProxy,
    private _userServiceProxy: UserServiceProxy,
    private _outsourcingServiceProxy: OutsouringServiceProxy,
    private _processOrderServiceProxy:ProcessingOrderServiceProxy,
    private _workflowServiceProxy: WorkflowServiceProxy) {

    super(injector);
  }

  getOutsourcingAgreedList(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this.primengTableHelper.showLoadingIndicator();

    this._outsourcingServiceProxy.getOutsourcingForList(
      this.filterText,
      //this.primengTableHelper.getSorting(this.dataTable),
      'code',
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      this.primengTableHelper.getSkipCount(this.paginator, event),
    ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.primengTableHelper.records = result.items;
      console.log(result);
      this.outsourcings = result.items;
      this.primengTableHelper.hideLoadingIndicator();

      _.forEach(result.items, viewItem => {
        let currentD = this.outsourcings.find((element) => (element.id === viewItem.id));
        if (viewItem.createdUser !== '' && viewItem.createdUser != '') {
          this._userServiceProxy.getUserForAll(viewItem.createdUser)
            .subscribe(userResult => {
              console.log(userResult);
              currentD.createdUserName = userResult.user.name;
            });
        } else {
          currentD.createdUser = '-';
          currentD.createdUserName = '-';
        }

        this._processOrderServiceProxy.getProcessingAmountForTaskId(viewItem.taskForModelId,viewItem.code).subscribe(result => {
          currentD.processingAmount = result.toString();
        });

      });
    });
  }
  Detail(records): void {
    let workflowName = records.outsourcingType === 0 ? '一般过程外协申请' : '关键过程外协申请';

    this.flowworkauditdetailmodel.show(workflowName, records.workflowId);
  }
  OpenProcesingList(code: string): void {
    this.processingOrderList.show(code);
  }
  OpenOutsourcingApplyChangeList(code: string, outsourcingType: number): void {
    this.OutsourcingApplyChangeList.show(code, outsourcingType);
  }
}
